const WxUtil = require('../../../utils/WxUtil.js')
const db = wx.cloud.database();
const innerAudioContext = wx.createInnerAudioContext()

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputVal: "",
    wordResult: null,
    showRecommend:false,
    recommendList:[]
  },

  //搜词 或 词组
  doSearch: function () {

    //抓取单词
    //shanbei.getWords();
    let that = this;
    let word = this.data.inputVal;

    if (this.isWord(word)) {
      db.collection('words').where({
        word: word
      }).get().then(res => {
        that.showWord(res.data);
      });
    }else{
      db.collection('word_phrase').where({
        word: word
      }).get().then(res => {
        that.showWord(res.data);
      });
    }
    
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  //监听单词输入
  inputTyping: function (e) {
    let that = this;
    let val = e.detail.value;
    this.setData({
      inputVal: val,
      showRecommend:true
    });

    //去查询单词
    let filter = {
      word:{
        $regex: "^"+val + '.*$'
      }
    };

    let dbName = "words";
    if (!this.isWord(val)) {
      dbName = "word_phrase";
    }

    //调用分页云函数
    wx.cloud.callFunction({
      name: 'pagination',
      data: {
        dbName: dbName,
        pageIndex:1,
        pageSize:10,
        filter: filter
      },
      success(res) {
        console.info(res);

        let recommendList = res.result.data;
        let word_over = null;
        recommendList = recommendList.map(function (item) {
          word_over = item.word;
          word_over = word_over.substring(that.data.inputVal.length, item.word.length);
          item.word_over = word_over;
          return item;
        });
        that.setData({
          recommendList: recommendList
        });
      },
      fail(e) {
        console.info("调用云函数失败");
        console.info(e);
      }
    });

  },

  //选中单词
  selectWord: function (e) {
    console.info(e);
    this.setData({
      showRecommend: false,
      inputVal:e.currentTarget.dataset.word
    });
    this.doSearch();
  },
  
  //显示单词
  showWord: function (wordArr) {
    if (null != wordArr && wordArr.length>0)
    {
      this.setData({
        wordResult:wordArr[0]
      });
    }
  },
  isWord: function (word) {
    //如果有空格说明是词组
    if (word.search(" ") != -1) {
      return false;
    }
    return true;
  },


  //点击播放录音
  gotoPlay: function (e) {
    var filePath = e.currentTarget.dataset.key;

    if (null == filePath || "" == filePath) {
      filePath = this.data.wordResult.audio;
    }
    innerAudioContext.src =filePath;
    console.info(filePath);
    innerAudioContext.play();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  onShareAppMessage: function() {

  }
})